; (function (window) {
    if (window.mxcad) {
        return;
    }
    var mxcad = window.mxcad = {};

    var scriptSrc = window.document.getElementsByTagName('script')[document.getElementsByTagName('script').length - 1].src;
    mxcad.src = scriptSrc;

    var jsName = scriptSrc.split('/')[scriptSrc.split('/').length - 1];
    mxcad.location = scriptSrc.replace(jsName, '');

    mxcad.initialize = function (id) {
        var s, classid, Sys = {}, ua = navigator.userAgent.toLowerCase();
        if (id == undefined) {
            throw Error('必须输入div ID属性值');
        }
        if (s = ua.match(/msie ([\d.]+)/)) {
            Sys.ie = s[1];
        } else if (s = ua.match(/trident\/([\d.]+)/)) {
            Sys.ie9 = s[1];
        } else if (s = ua.match(/firefox\/([\d.]+)/)) {
            Sys.firefox = s[1];
        } else if (s = ua.match(/chrome\/([\d.]+)/)) {
            Sys.chrome = s[1];
        } else if (s = ua.match(/opera.([\d.]+)/)) {
            Sys.opera = s[1];
        } else if (s = ua.match(/version\/([\d.]+).*safari/)) {
            Sys.safari = s[1];
        }

        var classid = "74A777F8-7A8F-4e7c-AF47-7074828086E2";
        
        if (Sys.ie || Sys.ie9) {//如果是IE或者IE9
            var mxObject = new Array();
            mxObject.push('<object id="' + id + '_MxDrawXCtrl"  classid="clsid:' + classid + '" codebase=" ' + mxcad.location + 'cab/MxDrawX52.CAB" width="100%" height="100%" align="left">');
            mxObject.push('<param name="_Version" value="65536">');
            mxObject.push('<param name="_ExtentX" value="24262">');
            mxObject.push('<param name="_ExtentY" value="16219">');
            mxObject.push('<param name="_StockProps" value="0">');
            mxObject.push('<param name="IsRuningAtIE" value="1">');
            mxObject.push('<param name="EnablePrintCmd" value="1">');
            mxObject.push('<param name="ShowCommandWindow" value="0">');
            mxObject.push('<param name="ShowToolBars" value="0">');
            mxObject.push('<param name="ShowModelBar" value="0">');
            mxObject.push('<param name="Iniset" value="CHROME=Y">');
            mxObject.push('<param name="ToolBarFiles" value="">');
            mxObject.push('<param name="ShowMenuBar" value="0">');
            mxObject.push('<param name="EnableUndo" value="0">');
            mxObject.push('<param name="ShowPropertyWindow" value="0">');
            mxObject.push('<param name="UserName" value="UU中铁第四勘察设计院集团有限公司UU">');
            mxObject.push('<param name="UserSoftwareName" value="UU辅助设计项目UU">');
            mxObject.push('<param name="UserPhone" value="027-88131809">');
            mxObject.push('<param name="UserData" value="HHHH282AB346CB4FB33268F82C067BD80C0998015277215224C6E03D672150AD5B7935F580A9AF13BFB3E8D10000282A9921CCB2559B29AA8668A4F44F729FCF78ECD5ECC637AF6CAFA012175521F212C444FEE4079E9A310000282AACDE5C6132C3F0E4FE7E1DC77B3B8F1A87AFA0AEF09071C470AC28576ED49157CB87A51767A579F40000282A66DDE560A65643A5A94C3FBE527CC2A851175F90868C7ED449440C00208DDA821241706634166F160000282A46ACBEF8A4B2B9B1B2016A0D93E55CC74DF0596260184E474F16CD0BB23223E86761B07641EABEEA0000282AF8FBDF1E987E466FD8A4C27D4BA6CBBE148F28FB6CDD94578743A7B4C0E77906EF39480ECD5969700000282AB08DFFEC54C71F3C8C9127A9F8B49FEEAB514F7AEA759E958A1483CABB4E6AE3DF293EF993049B850000282A807DABBBE795AED64446C4501536F51028763DDFF7B05A93FFEC4AC79148099301633E6FD61530AD0000282AFB732AF6F66339A891C6788A3D36372C62E471AA7F8B36054D1320037D08B097F4258B72945011DA0000282A9E232BA031C95F02A7414A25FC47DD314CFB961B5191C5AD639F860D9A51D8C90C1B6C81C56989480000">');
            mxObject.push('<param name="Iniset" value="NetLockServer=119.98.168.124">');
            mxObject.push('<param name="Event_ImplementCommandEvent" value="DoCommandEventFunc">');
            mxObject.push('<span style="color:red">不能装载文档控件。请在检查浏览器的选项中检查浏览器的安全设置。请点击<a href="">安装控件</a></span>'),
            mxObject.push('</object>');
            var parentDiv = window.document.getElementById(id);
            parentDiv.innerHTML = mxObject.join('');
            mxcad.mxOcx = window.document.getElementById(id + "_MxDrawXCtrl");
            return;
        }

        if (Sys.chrome) {//如果是chrome
            var mxObject = new Array();
            mxObject.push('<object id="' + id + '_MxDrawXCtrl" clsid="{' + classid + '}" type="application/mxdraw-activex" width="100%" height="100%" align="left">');
            mxObject.push('<param name="_Version" value="65536">');
            mxObject.push('<param name="_ExtentX" value="24262">');
            mxObject.push('<param name="_ExtentY" value="16219">');
            mxObject.push('<param name="_StockProps" value="0">');
            mxObject.push('<param name="IsRuningAtIE" value="1">');
            mxObject.push('<param name="EnablePrintCmd" value="1">');
            mxObject.push('<param name="ShowCommandWindow" value="0">');
            mxObject.push('<param name="ShowToolBars" value="0">');
            mxObject.push('<param name="ShowModelBar" value="0">');
            mxObject.push('<param name="Iniset" value="CHROME=Y">');
            mxObject.push('<param name="ToolBarFiles" value="">');
            mxObject.push('<param name="ShowMenuBar" value="0">');
            mxObject.push('<param name="EnableUndo" value="0">');
            mxObject.push('<param name="ShowPropertyWindow" value="0">');
            mxObject.push('<param name="UserName" value="UU中铁第四勘察设计院集团有限公司UU">');
            mxObject.push('<param name="UserSoftwareName" value="UU辅助设计项目">UU');
            mxObject.push('<param name="UserPhone" value="027-88131809">');
            mxObject.push('<param name="UserData" value="HHHH282AB346CB4FB33268F82C067BD80C0998015277215224C6E03D672150AD5B7935F580A9AF13BFB3E8D10000282A9921CCB2559B29AA8668A4F44F729FCF78ECD5ECC637AF6CAFA012175521F212C444FEE4079E9A310000282AACDE5C6132C3F0E4FE7E1DC77B3B8F1A87AFA0AEF09071C470AC28576ED49157CB87A51767A579F40000282A66DDE560A65643A5A94C3FBE527CC2A851175F90868C7ED449440C00208DDA821241706634166F160000282A46ACBEF8A4B2B9B1B2016A0D93E55CC74DF0596260184E474F16CD0BB23223E86761B07641EABEEA0000282AF8FBDF1E987E466FD8A4C27D4BA6CBBE148F28FB6CDD94578743A7B4C0E77906EF39480ECD5969700000282AB08DFFEC54C71F3C8C9127A9F8B49FEEAB514F7AEA759E958A1483CABB4E6AE3DF293EF993049B850000282A807DABBBE795AED64446C4501536F51028763DDFF7B05A93FFEC4AC79148099301633E6FD61530AD0000282AFB732AF6F66339A891C6788A3D36372C62E471AA7F8B36054D1320037D08B097F4258B72945011DA0000282A9E232BA031C95F02A7414A25FC47DD314CFB961B5191C5AD639F860D9A51D8C90C1B6C81C56989480000">');
            mxObject.push('<param name="Iniset" value="NetLockServer=119.98.168.124">');
            mxObject.push('<param name="Event_ImplementCommandEvent" value="DoCommandEventFunc">');
            mxObject.push('<span style="color:red">不能装载文档控件,谷歌浏览器使用,下载MxChrome安装包到本地，安装后点击桌面 Chrome企业版 快捷方式启动。请点击<a href="' + mxcad.location + 'cab/MxChromex86Setup(NetLock).exe">Chrome企业版</a></SPAN>');
            mxObject.push('</object>');
            var parentDiv = window.document.getElementById(id);
            parentDiv.innerHTML = mxObject.join('');
            mxcad.mxOcx = window.document.getElementById(id + "_MxDrawXCtrl");
            return;
        }

        if (Sys.firefox) {
            //安装ieTab插件
            return;
        }

        if (Sys.opera || Sys.safari) {
            return;
        }
    };

    mxcad.open = function (url) {
    	debugger;
        mxcad.execute(function () {
            mxcad.mxOcx.OpenWebDwgFile(url);
        });
    };

    mxcad.execute = function (f) {
    	debugger;
        var handler = setInterval(function () {
            if (mxcad.mxOcx) {
                if (!mxcad.mxOcx.IsIniting()) {
                    f();
                    clearInterval(handler);
                }
            }
        });
    };

})(window);